package com.thkj.controller;

import com.thkj.camera.CamerasRequest;
import com.thkj.camera.HikCamerasResoures;
import com.thkj.dao.DistrictMapper;
import com.thkj.dao.MainTypeMapper;
import com.thkj.dao.TownMapper;
import com.thkj.entity.*;
import com.thkj.service.*;
import com.thkj.util.RequestUtil;
import dp.R;
import dp.dao.PagingList;
import com.thkj.framework.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/backstage/details")
public class MapDetailsAction extends BaseController {

    @Autowired
    ViolationService violationService;

    @Autowired
    TblIndividualService tblIndividualService;

    @Autowired
    FaceStrangerService faceStrangerService;

    @Autowired
    BusinWorkerService businWorkerService;

    @Autowired
    LivingAlarmService livingAlarmService;
    @Autowired
    TemMeasuringService temMeasuringService;

    @Autowired
    TblMoniConfigService moniConfigService;

    @Autowired
    MainTypeMapper mainTypeMapper;
    @Autowired
    TownMapper townMapper;
    @Autowired
    DistrictMapper districtMapper;

    @Autowired
    MapDetailsService mapDetailsService;
    @Autowired
    DevicesService devicesService;
    @Autowired
    DisinfectRecordService disinfectRecordService;

    @RequestMapping("")
    public ModelAndView index(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("individual_id", id);
        return new ModelAndView("map/map_to_detail");
    }

    /**
     * 获取经营户详情
     *
     * @param individual_id //经营户ID  必填
     * @return
     */
    @RequestMapping("/getIndividualDetails")
    public R getIndividualDetails(@RequestParam(value = "individual_id") String individual_id) {
        TblIndividual individual = tblIndividualService.getTblIndividualById(individual_id);
        MainType mainType = mainTypeMapper.selectByPrimaryKey(individual.getMainType());
        individual.setMainTypeName(mainType.getMainType());
        District district = districtMapper.selectByPrimaryKey(individual.getDistrict());
        individual.setDistrictName(district.getDistrictName());
        Town town = townMapper.selectByPrimaryKey(individual.getTown());
        individual.setTownName(town.getTownName());
        return R.success(individual);
    }


    /**
     * 查看违规列表
     *
     * @param page            //页码   必填
     * @param size            //页数   必填
     * @param individual_id   //经营户ID  必填
     * @param violation_state //违规状态:1=未处理  2=已经处理  选填
     * @param violation_type  //类型 选填
     * @param device_id       //设备ID 选填
     * @return
     */
    @RequestMapping("/getViolationList")
    public R getViolationList(@RequestParam(value = "page", required = true) Integer page,
                              @RequestParam(value = "size", required = true) Integer size,
                              @RequestParam(value = "individual_id", required = true) String individual_id,
                              @RequestParam(value = "violation_state", required = false) Integer violation_state,
                              @RequestParam(value = "violation_type", required = false) Integer violation_type,
                              @RequestParam(value = "device_id", required = false) String device_id) {
        PagingList<Violation> query = violationService.query(page, size, individual_id, violation_state, violation_type, device_id);
        return R.success(query);
    }
    /**
     * 查看违规列表
     * @return
     */
    @RequestMapping("/queryViolationByTime")
    public R queryViolationByTime(HttpServletRequest request) {

        try {
            Map<String, Object> parameterMap = RequestUtil.getParameterMap(request);
            List<Map> query = violationService.queryViolationByTime(parameterMap);
            return R.success(query);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failure("异常");
        }
    }



    /**
     * 查询违规总数
     *
     * @param individual_id   //经营户ID  必填
     * @param violation_state //违规状态:1=未处理  2=已经处理  选填
     * @param type            //要查看:1=年  2=月(2020-03)  3=天(2020-03-12) 选填
     * @param timeStr         //时间:type=1 (2020)  type=2(2020-03) type=3(2020-03-12) 选填
     * @return
     */
    @RequestMapping("/getViolationTotal")
    public R getViolationTotal(@RequestParam(value = "individual_id", required = true) String individual_id,
                               @RequestParam(value = "violation_state", required = false) Integer violation_state,
                               @RequestParam(value = "type", required = false) Integer type,
                               @RequestParam(value = "timeStr", required = false) String timeStr) {
        Integer integer = violationService.queryTotal(individual_id, violation_state, type, timeStr);
        return R.success(integer);
    }


    /**
     * 获取违规统统计数据
     *
     * @param individual_id //经营户ID  必填
     * @param type          //1:年   2:月  3:本周  4:当天  选填
     * @param timeStr       // type=1(2020) type=2(2020-04)  type=3(不用填)  type=4(2020-04-27)
     * @return
     */
    @RequestMapping("/getViolationStatistics")
    public R getViolationStatistics(@RequestParam(value = "individual_id", required = true) String individual_id,
                                    @RequestParam(value = "type", required = false) Integer type,
                                    @RequestParam(value = "timeStr", required = false) String timeStr) throws Exception {
        Map<String, List<Object>> map = violationService.queryStatistics(individual_id, type, timeStr);
        return R.success(map);
    }

    //###########################################陌生人检查 start####################################################

    /**
     * 查看陌生人列表
     *
     * @param page          //页码   必填
     * @param size          //页数   必填
     * @param individual_id //经营户ID  必填
     * @param alerm_state   //状态:1=未处理  2=已经处理  选填
     * @param alerm_type    //类型 选填
     * @param device_id     //设备ID 选填
     * @return
     */
    @RequestMapping("/getFaceStrangerList")
    public R getFaceStrangerList(@RequestParam(value = "page", required = true) Integer page,
                                 @RequestParam(value = "size", required = true) Integer size,
                                 @RequestParam(value = "individual_id", required = true) String individual_id,
                                 @RequestParam(value = "alerm_state", required = false) Integer alerm_state,
                                 @RequestParam(value = "alerm_type", required = false) Integer alerm_type,
                                 @RequestParam(value = "device_id", required = false) String device_id) {
        PagingList<FaceStranger> query = faceStrangerService.query(page, size, individual_id, alerm_state, alerm_type, device_id);
        return R.success(query);
    }


    /**
     * 查询陌生人总数
     *
     * @param individual_id //经营户ID  必填
     * @param alerm_state   //状态:1=未处理  2=已经处理  选填
     * @param type          //要查看:1=年  2=月(2020-03)  3=天(2020-03-12) 选填
     * @param timeStr       //时间:type=1 (2020)  type=2(2020-03) type=3(2020-03-12) 选填
     * @return
     */
    @RequestMapping("/getFaceStrangerTotal")
    public R getFaceStrangerTotal(@RequestParam(value = "individual_id", required = true) String individual_id,
                                  @RequestParam(value = "alerm_state", required = false) Integer alerm_state,
                                  @RequestParam(value = "type", required = false) Integer type,
                                  @RequestParam(value = "timeStr", required = false) String timeStr) {
        Integer integer = faceStrangerService.queryTotal(individual_id, alerm_state, type, timeStr);
        return R.success(integer);
    }


    /**
     * 获取陌生人统统计数据
     *
     * @param individual_id //经营户ID  必填
     * @param type          //1:年   2:月  3:本周  4:当天  选填
     * @param timeStr       // type=1(2020) type=2(2020-04)  type=3(不用填)  type=4(2020-04-27)
     * @return
     */
    @RequestMapping("/getFaceStrangerStatistics")
    public R getFaceStrangerStatistics(@RequestParam(value = "individual_id", required = true) String individual_id,
                                       @RequestParam(value = "type", required = false) Integer type,
                                       @RequestParam(value = "timeStr", required = false) String timeStr) throws Exception {
        Map<String, List<Object>> map = faceStrangerService.queryStatistics(individual_id, type, timeStr);
        return R.success(map);
    }

    //###########################################陌生人检查 end####################################################


    //###########################################经营户的工作人员  start####################################################

    /**
     * 获取经营户的工作人员
     *
     * @param individual_id //经营户ID  必填
     * @param page          //页码  非必填
     * @param size          //页数  非填
     * @return
     */
    @RequestMapping("/getBusinWorkerList")
    public R getBusinWorkerList(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false) Integer size,
            @RequestParam(value = "individual_id") String individual_id) {
        PagingList<BusinWorker> query = businWorkerService.query(page, size, individual_id);
        return R.success(query);
    }
    //###########################################经营户的工作人员  end####################################################


    //###########################################活物检查  start####################################################

    /**
     * 查看活物检查列表
     *
     * @param page          //页码   必填
     * @param size          //页数   必填
     * @param individual_id //经营户ID  必填
     * @param alerm_state   //状态:1=未处理  2=已经处理  选填
     * @param alerm_type    //类型 选填
     * @param device_id     //设备ID 选填
     * @return
     */
    @RequestMapping("/getLivingList")
    public R getLivingList(@RequestParam(value = "page", required = true) Integer page,
                           @RequestParam(value = "size", required = true) Integer size,
                           @RequestParam(value = "individual_id", required = true) String individual_id,
                           @RequestParam(value = "alerm_state", required = false) Integer alerm_state,
                           @RequestParam(value = "alerm_type", required = false) Integer alerm_type,
                           @RequestParam(value = "device_id", required = false) String device_id) {
        PagingList<LivingAlarm> query = livingAlarmService.query(page, size, individual_id, alerm_state, alerm_type, device_id);
        return R.success(query);
    }


    /**
     * 查询活物总数  (老鼠 动物 )
     *
     * @param individual_id //经营户ID  必填
     * @param alerm_state   //状态:1=未处理  2=已经处理  选填
     * @param type          //要查看:1=年  2=月(2020-03)  3=天(2020-03-12) 选填
     * @param timeStr       //时间:type=1 (2020)  type=2(2020-03) type=3(2020-03-12) 选填
     * @return
     */
    @RequestMapping("/getLivingTotal")
    public R getLivingTotal(@RequestParam(value = "individual_id", required = true) String individual_id,
                            @RequestParam(value = "alerm_state", required = false) Integer alerm_state,
                            @RequestParam(value = "type", required = false) Integer type,
                            @RequestParam(value = "timeStr", required = false) String timeStr) {
        Integer integer = livingAlarmService.queryTotal(individual_id, alerm_state, type, timeStr);
        return R.success(integer);
    }


    /**
     * 获取活物统统计数据 (老鼠 动物 )
     *
     * @param individual_id //经营户ID  必填
     * @param type          //1:年   2:月  3:本周  4:当天  选填
     * @param timeStr       // type=1(2020) type=2(2020-04)  type=3(不用填)  type=4(2020-04-27)
     * @return
     */
    @RequestMapping("/getLivingStatistics")
    public R getLivingStatistics(@RequestParam(value = "individual_id", required = true) String individual_id,
                                 @RequestParam(value = "type", required = false) Integer type,
                                 @RequestParam(value = "timeStr", required = false) String timeStr) throws Exception {
        Map<String, List<Object>> map = livingAlarmService.queryStatistics(individual_id, type, timeStr);
        return R.success(map);
    }
    //###########################################活物检查  end####################################################


    //###########################################经营户的工作人员体温检查  start####################################################

    /**
     * 根据经营户获取工作人员的体温列表
     *
     * @param individual_id //必填
     * @param timeStr       //时间(2020-04-28)必填  按照天查看
     * @return
     * @throws Exception
     */
    @RequestMapping("/getWorkerTemList")
    public R getWorkerTemList(@RequestParam(value = "individual_id", required = true) String individual_id,
                              @RequestParam(value = "timeStr", required = true) String timeStr) throws Exception {
        List<Map> maps = businWorkerService.queryByIndividualId(individual_id, timeStr);
        return R.success(maps);
    }

    /**
     * 获取体温统计
     *
     * @param individual_id // 经营户 必填
     * @param type          //1:年  2:月  3:天   4本周
     * @param timeStr       //type=1(2020)  type=2(2020-04) type=3(2020-04-28) type=4(b不用填写)
     * @return
     * @throws Exception
     */
    @RequestMapping("/getTemStatistics")
    public R getTemStatistics(@RequestParam(value = "individual_id", required = true) String individual_id,
                              @RequestParam(value = "type", required = true) Integer type,
                              @RequestParam(value = "timeStr", required = false) String timeStr) throws Exception {
        List<Map> list = temMeasuringService.queryTotal(individual_id, type, timeStr);
        return R.success(list);
    }
    //###########################################经营户的工作人员体温检查  end####################################################


    //###########################################获取经营户的摄像头  start####################################################

    /**
     * 根据individual_id 查看寻经营户的所有摄像头
     *
     * @param individual_id //经营户ID  必填
     * @return
     */
    @RequestMapping("/getCameraDevices")
    public R getCameras(@RequestParam("individual_id") String individual_id) {

        TblCameraRele cameraRele = moniConfigService.getByIndividualId(individual_id);
        if (cameraRele == null) {
            return R.failure("未查询到数据!");
        }

        CamerasRequest camerasRequest = new CamerasRequest();
        camerasRequest.setPageNo(1);
        camerasRequest.setPageSize(200);
        camerasRequest.setRegionIndexCode(cameraRele.getIndexCode());
        camerasRequest.setTreeCode(cameraRele.getTreeCode());
        List<Map> cameras = HikCamerasResoures.getCameras(camerasRequest);
        return R.success(cameras);
    }
    //###########################################获取经营户的摄像头  end####################################################

    //###########################################设备相关的 start####################################################

    /**
     * 获取所有设备种类 和 数量
     *
     * @param individual_id //经营户ID  必填
     * @return
     */
    @RequestMapping("/getDeviceCountAndType")
    public R getDeviceCountAndType(@RequestParam("individual_id") String individual_id) {
        Map<String, List<Object>> deviceCountAndType = mapDetailsService.getDeviceCountAndType(individual_id);
        return R.success(deviceCountAndType);
    }
    //###########################################设备相关的 end####################################################


    //###########################################消毒餐具相关 start####################################################

    /**
     * 根据经营户查询所有消毒设备列表
     *
     * @param individual_id //经营户ID  必填
     * @param timeStr       //时间(2020-04-28)必填  按照天查看
     *                      //返回state  state=1 合格  state=2 不合格
     * @return [{"duration":8.03,"name":"1号柜","timeStr":"2020-04","id":"1"},{"duration":8.03,"name":"2号柜","timeStr":"2020-04","id":"2"},{"duration":8.03,"name":"3号柜","timeStr":"2020-04","id":"3"},{"duration":8.03,"name":"4号柜","timeStr":"2020-04","id":"4"},{"duration":8.03,"name":"5号柜","timeStr":"2020-04","id":"5"}]
     */
    @RequestMapping("/getDisinfectByIndividualId")
    public R getDisinfectByIndividualId(@RequestParam("individual_id") String individual_id,
                                        @RequestParam(value = "timeStr") String timeStr) {
        List<Map> list = devicesService.queryDisinfectByIndividualId(individual_id, timeStr);
        return R.success(list);
    }

    /**
     * @param individual_id // 经营户ID  必填
     * @param type          //1:年  2:月  3:天   4本周
     * @param timeStr       //type=1(2020)  type=2(2020-04) type=3(2020-04-28) type=4(b不用填写)
     * @return [{"设备总数":5},{"消毒总时长":19.5},{"总检查天数":15},{"合格总数":5},{"不合格总数":10}]
     */
    @RequestMapping("/getDisinfectStatistics")
    public R getDisinfectStatistics(@RequestParam("individual_id") String individual_id,
                                    @RequestParam("type") Integer type,
                                    @RequestParam(value = "timeStr") String timeStr) {
        List<Map> map = disinfectRecordService.queryDisinfectStatistics(individual_id, type, timeStr);
        return R.success(map);
    }

    //###########################################消毒餐具相关 end####################################################

    //###########################################达标统计 start####################################################

    /**
     * 获取达标率
     *
     * @param individual_id // 经营户ID  必填
     * @param type          //1:年  2:月    4本周
     * @param timeStr       //type=1(2020)  type=2(2020-04) type=3(2020-04-28) type=4(b不用填写)
     * @return //  [{"设备总数":5},{"消毒总时长":19.5},{"总检查天数":15},{"合格总数":5},{"不合格总数":10}]
     */
    @RequestMapping("/getControlRatesStatistics")
    public R getControlRatesStatistics(@RequestParam("individual_id") String individual_id,
                                       @RequestParam("type") Integer type,
                                       @RequestParam(value = "timeStr") String timeStr) {
        Map<String, List<Object>> map = mapDetailsService.querControlRatesStatistics(individual_id, type, timeStr);
        return R.success(map);
    }

    //###########################################达标统计 end####################################################

}